{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.468691700Z",
     "start_time": "2024-05-18T02:48:50.462188600Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from torch.utils import data\n",
    "from d2l import torch as d2l\n",
    "\n",
    "true_w = torch.tensor([2, -3.4])\n",
    "true_b = 4.2\n",
    "features, labels = d2l.synthetic_data(true_w, true_b, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "<torch.utils.data.dataloader.DataLoader at 0x22299215290>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_iter = d2l.load_array((features, labels), batch_size=10)\n",
    "data_iter"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.473788Z",
     "start_time": "2024-05-18T02:48:50.464691600Z"
    }
   },
   "id": "43236ccf012bf244"
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.7684,  1.3812],\n",
      "        [ 0.7246,  0.9481],\n",
      "        [ 0.5222, -0.4238],\n",
      "        [-1.1653, -0.2456],\n",
      "        [ 1.7635, -0.4393],\n",
      "        [-1.8262,  0.2739],\n",
      "        [-0.9057,  0.3792],\n",
      "        [-1.2954, -0.1005],\n",
      "        [-0.3351, -0.3635],\n",
      "        [-0.5302,  0.1715]]) tensor([[ 3.0371],\n",
      "        [ 2.4300],\n",
      "        [ 6.6906],\n",
      "        [ 2.7014],\n",
      "        [ 9.2029],\n",
      "        [-0.3846],\n",
      "        [ 1.1013],\n",
      "        [ 1.9647],\n",
      "        [ 4.7723],\n",
      "        [ 2.5393]])\n"
     ]
    }
   ],
   "source": [
    "for item in data_iter:\n",
    "    a, b = item\n",
    "    print(a,b)\n",
    "    break"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.495946600Z",
     "start_time": "2024-05-18T02:48:50.473788Z"
    }
   },
   "id": "bad01e54a487ce71"
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.0138, -0.0023]])\n",
      "tensor([0.])\n"
     ]
    }
   ],
   "source": [
    "from torch import nn\n",
    "linear = nn.Linear(2, 1)\n",
    "linear.weight.data.normal_(0, 0.01)\n",
    "linear.bias.data.fill_(0)\n",
    "\n",
    "print(linear.weight.data)\n",
    "print(linear.bias.data)\n",
    "net = nn.Sequential(\n",
    "    linear\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.496946500Z",
     "start_time": "2024-05-18T02:48:50.483711200Z"
    }
   },
   "id": "fdf7efeb73d1a6c5"
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [],
   "source": [
    "loss = nn.MSELoss()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.502945500Z",
     "start_time": "2024-05-18T02:48:50.494946300Z"
    }
   },
   "id": "39a412afcfd3cfc6"
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [],
   "source": [
    "trainer = torch.optim.SGD(net.parameters(), lr=0.03)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.510908100Z",
     "start_time": "2024-05-18T02:48:50.502945500Z"
    }
   },
   "id": "3d43871a3315efc3"
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, loss 0.000319\n",
      "epoch 2, loss 0.000093\n",
      "epoch 3, loss 0.000093\n"
     ]
    }
   ],
   "source": [
    "num_epoch = 3\n",
    "for epoch in range(num_epoch):\n",
    "    for x, y in data_iter:\n",
    "        l = loss(net(x), y)\n",
    "        trainer.zero_grad()\n",
    "        l.backward()\n",
    "        trainer.step()\n",
    "    l = loss(net(features), labels)\n",
    "    print(f\"epoch {epoch + 1}, loss {l:f}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.606487600Z",
     "start_time": "2024-05-18T02:48:50.510908100Z"
    }
   },
   "id": "b7b0b2237d410cb4"
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-18T02:48:50.610416100Z",
     "start_time": "2024-05-18T02:48:50.607418700Z"
    }
   },
   "id": "d26d466ca74d4fea"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
